#include "GlWidget.h"
#include "GlGuiEventRecognizer.h"


/**************************************************************************************************
 * <summary>  Number of Slots preserved for basic action chains. </summary>
 *
 * <remarks>  Deng Nianchen, 2011/3/21. </remarks>
 **************************************************************************************************/

#define BASIC_ACTION_CHAIN_PERSERVE 100


GlWidget::GlWidget (GlWidget * pParent) : _pParent (pParent)
{
  if (pParent != NULL)
    pParent->_appendChild (this);
}



GlWidget::~GlWidget ()
{
  for (size_t i = 0; i < _pChildren.size (); ++i)
  {
    delete _pChildren[i];
  }
  for (size_t i = 0; i < _pBasicActionChains.size (); ++i)
  {
    delete _pBasicActionChains[i];
  }
  for (size_t i = 0; i < _pCustomActionChains.size (); ++i)
  {
    delete _pCustomActionChains[i];
  }
}



void GlWidget::draw () const
{
  throw std::exception("The method or operation is not implemented.");
}



void GlWidget::addAction (int eventType, GlAction * pAction) 
{
  if (eventType >= BASIC_ACTION_CHAIN_PERSERVE)
  {
    int customSlot = eventType - BASIC_ACTION_CHAIN_PERSERVE;
    _pCustomActionChains[customSlot]->addAction (pAction);
  }
  else
  {
    _pBasicActionChains[eventType]->addAction (pAction);
  }
}



void GlWidget::registerActionsTo (GlGuiEvent & event) 
{
  GlGuiEventRecognizer & eventRecognizer
    = GlGuiEventRecognizer::getSingleton ();
  for (size_t i = 0; i < _pBasicActionChains.size (); ++i)
  {
    if (eventRecognizer.shouldEventRaised (i, this))
      event.registerActionChain (_pBasicActionChains[i]);
  }
  
}



void GlWidget::_appendChild (GlWidget * pWidget) 
{
  if (pWidget != NULL)
    _pChildren.push_back (pWidget);
}



GlString GlWidget::getTitle () const
{
  throw std::exception("The method or operation is not implemented.");
}



void GlWidget::setTitle (const GlString & title)
{

}



const GlWidgetStyle & GlWidget::getStyle () const
{
  throw std::exception("The method or operation is not implemented.");
}



GlWidgetStyle & GlWidget::getStyle ()
{

}



void GlWidget::setStyle (const GlWidgetStyle & style)
{
  
}



const vector< GlWidget * > & GlWidget::getChildren () const
{
  return _pChildren;
}



vector< GlWidget * > & GlWidget::getChildren ()
{
  return _pChildren;
}